<?php
/**LinkSlideText V 0.3 - www.devedition.com**/
class Linkslidetext extends Module
{
	protected $maxImageSize = 307200;

 	function __construct()
 	{
 	 	$this->name = 'linkslidetext';
 	 	$this->tab = 'Tools';
        $this->version = '0.3';

		parent::__construct();

        $this->page = basename(__FILE__,'.php');
        $this->displayName = $this->l('Feature Product');
        $this->description = $this->l('Display feature product in homepage');
 	}

 	function install()
 	{
        if (!parent::install() OR !$this->registerHook('home') OR !$this->registerHook('header'))
           return false;
        return true;
  	}

	private function _displayLinkslide()
	{
		$this->_html .= '<a href="http://www.devedition.com" target="_blank"><img src="../modules/linkslidetext/pack/devedition.jpg" style="float:left; margin-right:15px;"></a><b>
			'.$this->l('Version 0.3.').'</b><br /><br /><br /><br /><br />';
	}

	function putContent($xml_data, $key, $field)
	{
		$field = htmlspecialchars($field);
		if (!$field)
			return 0;
		return ("\n" . '		<' . $key . '>' . $field . '</' . $key . '>');
	}

	public function getContent()
	{
		$this->_html = '<h2>' . $this->displayName . '</h2>';

		if (isset($_POST['submitUpdate'])) {
			$newXml = '<?xml version=\'1.0\' encoding=\'utf-8\' ?>' . "\n";
			$newXml .= '<links>' . "\n";
			$i = 0;
			foreach ($_POST['link'] as $link) {
				$newXml .= '	<link>';
				foreach ($link AS $key => $field) {
					if ($line = $this->putContent($newXml, $key, $field))
						$newXml .= $line;
				}
				if (isset($_FILES['link_' . $i . '_img']) AND isset($_FILES['link_' . $i . '_img']['tmp_name']) AND !empty($_FILES['link_' . $i . '_img']['tmp_name'])) {
					Configuration::set('PS_IMAGE_GENERATION_METHOD', 1);
					if ($error = checkImage($_FILES['link_' . $i . '_img'], $this->maxImageSize))
						$this->_html .= $error;
					elseif (!$tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS') OR !move_uploaded_file($_FILES['link_' . $i . '_img']['tmp_name'], $tmpName))
						return false;
					elseif (!imageResize($tmpName, dirname(__FILE__) . '/link' . $i . '.jpg'))
						$this->_html .= $this->displayError($this->l('An error occurred during the image upload.'));
					unlink($tmpName);
				}
				if ($line = $this->putContent($newXml, 'img', 'link' . $i . '.jpg'))
					$newXml .= $line;
				$newXml .= "\n" . '	</link>' . "\n";
				$i++;
			}
			$newXml .= '</links>' . "\n";

			if ($fd = @fopen(dirname(__FILE__) . '/links.xml', 'w')) {
				if (!@fwrite($fd, $newXml))
					$this->_html .= $this->displayError($this->l('Unable to write to the editor file.'));
				if (!@fclose($fd))
					$this->_html .= $this->displayError($this->l('Can\'t close the editor file.'));
			}else
				$this->_html .= $this->displayError($this->l('Unable to update the editor file.<br />Please check the editor file\'s writing permissions.'));
		}
        //$this->_displayLinkslide();
		$this->_displayForm();

		return $this->_html;
	}

	public function _displayForm()
	{
		$defaultLanguage = intval(Configuration::get('PS_LANG_DEFAULT'));
		$languages = Language::getLanguages();
		$iso = Language::getIsoById($defaultLanguage);

		$xml = false;
		if (file_exists(dirname(__FILE__) . '/links.xml'))
			if (!$xml = @simplexml_load_file(dirname(__FILE__) . '/links.xml'))
				$this->_html .= $this->displayError($this->l('Your links file is empty.'));

		$this->_html .= '<br />
			<form method="post" action="' . $_SERVER['REQUEST_URI'] . '" enctype="multipart/form-data">
				<fieldset style="width: 900px;">
					<legend><img src="' . $this->_path . 'logo.gif" alt="" title="" /> ' . $this->displayName . '</legend>';
		$i = 0;
		foreach ($xml->link as $link) {
			if($i<2){
			$divLangName = 'title'.$i.'&curren;cpara'.$i;
			$this->_html .= '<h3>' . $this->l('Product #') . ($i+1) .'</h3>';

			$this->_html .= '<label>' . $this->l('Picture') . ' </label>
					<div class="margin-form">
					<img src="' . $this->_path . 'link' . $i . '.jpg" alt="" title="" style="" /><br />
					<input type="file" name="link_' . $i . '_img" /> 320x215 pixel
					<p style="clear: both"></p>
					</div>
					<label>' . $this->l('Link') . '</label>
					<div class="margin-form">
					<input type="text" name="link[' . $i . '][url]" size="64" value="' . ($xml ? stripslashes(htmlspecialchars($xml->link[$i]->url)) : '') . '" />  ต้องมี http:// ด้วย  เช่น http://www.google.co.th
					<p style="clear: both"></p>
					</div>
					<div class="clear pspace"></div>';
			$i++;
			}
		}
		    $this->_html .= '
						<div class="margin-form clear"><input type="submit" name="submitUpdate" value="' . $this->l('Update the editorial') . '" class="button" /></div>
			</fieldset>
		</form>';
	}

	function hookHeader($params)
	{
		global $smarty;
		$smarty->assign(array(
		        'this_path' => $this->_path
		        ));
		return $this->display(__FILE__, 'linksheader.tpl');
	}

	function hookHome($params)
	{
		if (file_exists(dirname(__FILE__) . '/links.xml'))
			if ($xml = simplexml_load_file(dirname(__FILE__) . '/links.xml')) {
				global $cookie, $smarty;
				$smarty->assign(array(
				        'xml' => $xml,
				        'title' => 'title_' . $cookie->id_lang,
				        'text' => 'text_' . $cookie->id_lang,
				        'this_path' => $this->_path
				        ));
				return $this->display(__FILE__, 'linkslidetext.tpl');
			}
		return false;
	}

}

?>